
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// 00 FOLDER SETTINGS
** Definition of Start Point
global 		path 	""
			
set maxvar 32767	

** Input data folder
global bhc_in 		"$path/0 Data/BHCY9C/Input"
global crsp_in		"$path/0 Data/CRSP"
global fred_in		"$path/0 Data/Fred"
global snl_in		"$path/0 Data/SNL"
global ffiec_in		"$path/0 Data/FFIEC/Input"
global wrds_in		"$path/0 Data/FFIEC/WRDS"


** Output data folder 
global bhc_out 		"$path/0 Data/BHCY9C/Output"
global ffiec_out	"$path/0 Data/FFIEC/Output"

** Processed data folder 
global processed 	"$path/0 Data/_Processed"

* clear
clear
clear 		mata
mat 		drop _all


/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// 01 Stock prices and returns 2020 for motivating figure


* Note: Stock prices downloaded from the mergerd CRSP/Compustat database, annual updates, daily security prices. Download from 1.1.-31.12.2021

** Load Data
use "$crsp_in/CRSP_2020_Daily", clear


renam GVKEY gvkey
rename	tic Ticker

	
merge 	n:1 Ticker using "$path/0 Data/_Processed/temp_ticker_snl"
	
drop 	if _merge == 2   // 100 not matched (check later)
ren _merge _merge_snl
	
	
* Industry
destring sic, replace
gen sic_1 = int(sic / 1000)
drop if sic_1 == 0 | sic_1 == 9. // drop utilities 


* Banks vs. non-banks (sic code 6)
gen bank = (_merge_snl == 3)
sum bank


* Set time-series
egen firm_id = group(gvkey iid)
xtset firm_id datadate


* Adjust prices and shares outstandign for stock splits etc. (using AJEXDI)
gen	prc_adj	= prccd / ajexdi 
gen	cshoc_adj = cshoc * ajexdi


by firm_id: gen return = log(prc_adj[_n])-log(prc_adj[_n-1])
by firm_id: gen	mv = prc_adj * cshoc_adj
	
	
* Drops
gsort firm_id -datadate
gen mv_sort = mv
by firm_id: replace mv_sort = mv_sort[_N]
drop if mv_sort	<	10000000
	
drop		if Ticker == ""

* Winsorize on daily basis (this code is slow)		
egen group = group(datadate)
gen w_ret = .
su group, meanonly
forval i  = 1/`r(max)' {
capture { 
	winsor return if group == `i', gen(work) p(0.01)
	replace w_ret = work if group == `i'
	drop work
   }
}

* save file
save "$processed/crsp_2020_figure", replace


//////////////////////////////////////////////////////
///////// 2. Figure 1 Panel B

* Load data 
use "$path/0 Data/_Processed/crsp_2020_figure", clear


destring gvkey, replace
 

merge m:m gvkey using "$path/0 Data/_Processed/sample"

drop if _merge == 2
drop bank
gen bank = (_merge == 3)


* identify banks in the group of corporates (as they are banks but not in our sample (of 147 banks))
*(https://www.sec.gov/corpfin/division-of-corporation-finance-standard-industrial-classification-sic-code-list)
gen sic_drop = 1 if (inlist(sic, 6020, 6035, 6036,6099))

drop if (_merge == 1 & sic_drop == 1) // drop banks that are in the corporate sector but actually banks (but not in our sample)

replace bank = 3 if (_merge == 1 & sic_1 == 6) // identify the non-bank financial firms


* Collapse to day
collapse  (mean) return w_ret [aweight = mv], by(bank datadate)

generate index2 = .
replace	index2 = 100 if datadate == td(02jan2020)
by  bank (datadate), sort: replace index2 = index2[_n-1] * (1+w_ret) if index2 == .

tsset bank datadate

* Figure 
twoway (tsline index2 if bank==1) (tsline index2 if bank==0) (tsline index2 if bank==3), ///
	legend(label(1 "Banks") label(2 "Non-Financial Firms") label(3 "Non-Bank Financial Firms")) ///
 	scheme(s2mono) ///
	ylabel(, angle(horizontal)) ///
	xlabel(`=d(1feb2020)' `=d(1apr2020)' `=d(1june2020)' `=d(1aug2020)' `=d(1oct2020)' `=d(1dec2020)' , format(%tdMon-CCYY)) ///
	xtitle("Date")		///
	ytitle("Stock Price")		///
	ylabel(, angle(horizontal)) ///
	note("")   ///			
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
	graph export  "$path/05 Figures/Figure 1 Panel D.png",replace						
		
* Note: changed on 7 Jan 2022	
